package cc.eddic.pratice.programmingparadigms.exam.impl;

import cc.eddic.pratice.programmingparadigms.exam.Model.AnswerModel;
import cc.eddic.pratice.programmingparadigms.exam.QuestionBankService;
import cc.eddic.pratice.programmingparadigms.exam.QuestionType;
import cc.eddic.pratice.programmingparadigms.exam.domain.Category;
import cc.eddic.pratice.programmingparadigms.exam.domain.Option;
import cc.eddic.pratice.programmingparadigms.exam.domain.Question;
import cc.eddic.pratice.programmingparadigms.exam.domain.SubCategory;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;

import java.util.*;
import java.util.stream.Collectors;

public class QuestionBankServiceCxmImpl extends QuestionBankServiceImpl implements QuestionBankService {
    @Override
    public List<Question> importQuestions(String questionTextFile) {

        String fileContent = getQuestionFileContent();
        String[] file = fileContent.split("\n");
        QuestionType[] types = QuestionType.values();
        List<Question> questionList = new ArrayList<>();
        boolean isCorrect = false;
        int index = 0;
        for (int i = 0; i < file.length; i = i + 6) {
            String content = file[i].substring(file[i].indexOf("、") + 1);
            Question question = new Question(index + 1, content, types[0]);
            for (int j = 1; j < 5; j++){
                String answer = file[i+j].substring(0,1);
                String choice = file[i+j].substring(2);
                char[] ans = answer.toCharArray();
                Option option = new Option(choice, ans[0], isCorrect);
                question.add(option);
            }
            index ++;
            questionList.add(question);
        }
        //}
        return questionList;
    }

    @Override
    public List<Question> importCorrectAnswers(String correctAnswerTextFile) {
        return null;
    }

    @Override
    public List<AnswerModel> readCorrectAnswers(String correctAnswerTextFile) {
        return null;
    }

    @Override
    public List<Question> importCategories(String categoryExcelFile) {

        List<HashMap<Integer, String>> excelHeadMap=new ArrayList<>();
        List<HashMap<Integer,String>> mapList=new ArrayList<>();


        EasyExcel.read(categoryExcelFile, new AnalysisEventListener<Map<Integer,String>>() {
            @Override
            public void invoke(Map<Integer,String> integerStringMap, AnalysisContext analysisContext) {
                integerStringMap.remove(0);
                mapList.add((HashMap<Integer, String>) integerStringMap);
            }
            @Override
            public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
                headMap.remove(0);
                excelHeadMap.add((HashMap<Integer, String>) headMap);
            }
            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {

            }
        }).sheet(0).headRowNumber(2).doRead();
        HashMap<Integer,String> catogoryMap=excelHeadMap.get(0);
        HashMap<Integer,String> subCatogoryMap=excelHeadMap.get(1);

        String[] categoryNameArr = new ArrayList<>(catogoryMap.values()).toArray(new String[catogoryMap.values().size()]);
        String[] subCategoryNameArr = new ArrayList<>(subCatogoryMap.values()).toArray(new String[subCatogoryMap.values().size()]);
        List<Category> categoryList=new ArrayList<>();
        List<SubCategory> subCategoryList=new ArrayList<>();
        int i=0,j=0;
        while(i < categoryNameArr.length){
            if(categoryNameArr [i] != null){
                Category category=new Category(categoryNameArr[i]);
                categoryList.add(category);
                if(j < subCategoryNameArr.length){
                    category.add(new SubCategory(subCategoryNameArr[j++]));
                }
                int step=i;
                while( step < categoryNameArr.length && j < subCategoryNameArr.length && categoryNameArr[step+1] == null ){
                    category.add(new SubCategory(subCategoryNameArr[j++]));
                    step++;
                }
            }
            i++;
        }
        for(Map.Entry<Integer,String> entry:subCatogoryMap.entrySet()){
            subCategoryList.add(new SubCategory(entry.getValue()));
        }
        mapList.remove(mapList.size()-1);

        int no=0;
        List<Question> questionList =  importQuestions("");
        for(HashMap<Integer,String> map:mapList){
            HashMap<Integer,String> collect=(HashMap<Integer, String>) map.entrySet().stream().filter(e-> e.getValue() != null).collect(Collectors.toMap(
                    Map.Entry::getKey,
                    Map.Entry::getValue
            ));
            for(Integer key:collect.keySet()){
                for (Map.Entry<Integer, String> entry : subCatogoryMap.entrySet()) {
                    if (key.equals(entry.getKey())) {
                        questionList.get(no).setSubCategory(subCategoryList.stream().filter(subCategory -> subCategory.getName().equals(entry.getValue())).findFirst().orElse(null));
                        no++;
                    }
                }
            }
        }

        return questionList;
    }
}
